import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:tot_android_flutter/eneity/AppResponse.dart';
import 'package:tot_android_flutter/utils/JsonUtil.dart';
import 'package:tot_android_flutter/utils/dio_util/dio_util.dart';
import 'package:tot_android_flutter/utils/log_util.dart';

import '../constants/constants.dart';
import '../utils/dio_util/dio_method.dart';

class NetworkManager {
  factory NetworkManager() => _getInstance();
  static NetworkManager? _instance = NetworkManager._initialize();

  static NetworkManager _getInstance() {
    _instance ??= NetworkManager._initialize();
    return _instance!;
  }

  NetworkManager._initialize() {
    DioUtil.getInstance()?.openLog();
  }



  Future<AppResponse<T>> post<T>(
      String path, {
        Map<String, dynamic>? params,
        data,
        CancelToken? cancelToken,
        Options? options,
        ProgressCallback? onSendProgress,
        ProgressCallback? onReceiveProgress,
      }) async {
    try {
      // 调用原始的 request 方法
      var response = request<T>(path,
          method: DioMethod.post,
          params: params,
          data: data,
          cancelToken: cancelToken,
          options: options,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress);
      return response;
    } catch (e) {
      // 在这里可以添加额外的错误处理逻辑
      print('Error in proxyRequest: $e');
      rethrow;
    }
  }


  Future<AppResponse<T>> get<T>(
      String path, {
        Map<String, dynamic>? params,
        data,
        CancelToken? cancelToken,
        Options? options,
        ProgressCallback? onSendProgress,
        ProgressCallback? onReceiveProgress,
      }) async {
    try {
      // 调用原始的 request 方法
      var response = request<T>(path,
          method: DioMethod.get,
          params: params,
          data: data,
          cancelToken: cancelToken,
          options: options,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress);
      return response;
    } catch (e) {
      // 在这里可以添加额外的错误处理逻辑
      print('Error in proxyRequest: $e');
      rethrow;
    }
  }
  ///网络请求
  Future<AppResponse<T>> request<T>(
    String path, {
    DioMethod method = DioMethod.get,
    Map<String, dynamic>? params,
    data,
    CancelToken? cancelToken,
    Options? options,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    try {
      // 在这里可以添加额外的逻辑，比如日志记录
      print('Making request to path: $path with method: $method');
      // 调用原始的 request 方法
      var response = await DioUtil.getInstance().request<T>(path,
          method: method,
          params: params,
          data: data,
          cancelToken: cancelToken,
          options: options,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress);
      return response;
    } catch (e) {
      // 在这里可以添加额外的错误处理逻辑
      print('Error in proxyRequest: $e');
      rethrow;
    }
  }
}
